﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Jzy.Workflow.Node;

using Jzy.Workflow.Task;
using Jzy.Workflow.Snap;
using Frame.Lib.Utilities;
using Frame.Lib.Core.Tree;
using Frame.Lib.Core.User;

namespace Jzy.Web.WorkFlow.Fore
{
    public partial class SelectNodeUser : PageSelectTree
    {
        private bool SingleSelect
        {
            get
            {
                if (string.IsNullOrEmpty(Request.QueryString["SingleSelect"]))
                {
                    return true;
                }
                else
                {
                    return bool.Parse(Request.QueryString["SingleSelect"]);
                }
            }
        }

        private string NodeID
        {
            get
            {
                return Request.QueryString["NodeID"];
            }
        }

        private string TaskID
        {
            get
            {
                return Request.QueryString["TaskID"];
            }
        }
        private FlowNodeBO _node = new FlowNodeBO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindTree();
                if (this.SingleSelect)
                {
                    tv.Attributes.Add("onclick", "SingleSelect()");
                }
            }
        }

        private void BindTree()
        {
            List<UserInfo> users = null;
            if (string.IsNullOrEmpty(TaskID))
            {
                users = _node.GetAllUsers(NodeID);
            }
            else
            {
                UserBO user = new UserBO();

                FlowTaskInfo taskInfo = (new FlowTaskBO()).GetInfo(TaskID);

                FlowNodeInfo nodeInfo = new FlowNodeInfo(taskInfo.FormInfo.SnapInfo, int.Parse(NodeID));//找到存在Xml里的节点

                List<int> userIDs = nodeInfo.AllUserIDs;

                users = user.GetList(userIDs);
            }
            foreach (UserInfo info in users)
            {                
                TreeNode node = new TreeNode();
                node.ShowCheckBox = false;
                node.Text = TreeBO<TreeInfo>.GetCheckBoxNode(info.ID, info.Name, null);

                node.SelectAction = TreeNodeSelectAction.None;
                
                tv.Nodes.Add(node);
            }

            List<int> lstSelectIDs = StrUtil.SplitToInt(RestoreIDs);

            JSUtil.SetTreeSelect(lstSelectIDs);
        }
    }
}
